<template>
  <div class="upload-container">

    <div v-if="imageUrl.length>0" class="image-preview">
      <div v-for="(item,index) in imageUrl" :key="index" class="image-preview-wrapper" :style="{width:width,margin:margin}">
        <img :src="item">
        <div class="image-preview-action" @click="rmImage(index)">
          <i class="el-icon-delete" />
        </div>
      </div>
    </div>

    <el-upload
      v-if="!singleImage || imageUrl.length <1"
      :data="{authorization:upyun.authorization,policy:upyun.policy}"
      :multiple="multiple"
      :show-file-list="false"
      :on-success="handleImageSuccess"
      class="image-uploader"
      drag
      :action="`${upyun.upurl}`"
      :before-upload="beforeUpload"
      :style="{marginTop:imageUrl.length<1?'10px':'40px',width: uploadWidth}"
    >
      <i class="el-icon-upload" />
      <div class="el-upload__text">
        将文件拖到此处，或<em>点击上传</em>
      </div>
    </el-upload>

  </div>
</template>

<script>
import Upyun from '@/utils/upyun'
import { getDouHao } from '@/utils/util'

export default {
  name: 'SingleImageUpload',
  props: {
    value: {
      type: String,
      default: ''
    },
    remotePath: {
      type: String,
      default: 'testProject/{filemd5}{.suffix}'
    },
    singleImage: {
      type: Boolean,
      required: false,
      default() {
        return false
      }
    },
    margin: {
      type: String,
      required: false,
      default() {
        return '0'
      }
    },
    width: {
      type: String,
      required: false,
      default() {
        return '200px'
      }
    },
    multiple: {
      type: Boolean,
      required: false,
      default() {
        return false
      }
    }
  },
  data() {
    return {
      tempUrl: '',
      dataObj: { token: '', key: '' },
      upyun: new Upyun()
    }
  },
  computed: {
    imageUrl: {
      get() {
        if (this.value && this.value !== 0) {
          return this.getDouHao(this.value, [])
        }
        // return image
        return []
      },
      set() {
        console.log('执行set函数')
      }
    },
    uploadWidth() {
      if (this.width.includes('px')) {
        return parseInt(this.width) * 2 + 'px'
      }
      if (this.width.includes('%')) {
        return this.width
      }
      if (this.width.includes('auto')) {
        return 'auto'
      }
      return '400px'
    }
  },
  methods: {
    getDouHao,
    async beforeUpload(file) {
      await this.upyun.getSignature({ remotePath: this.remotePath })
      return true
    },
    rmImage(index) {
      this.imageUrl.splice(index, 1)
      this.$emit('input', this.imageUrl.join(','))
      this.$forceUpdate()
    },
    handleImageSuccess(res, file) {
      this.imageUrl.push(this.upyun.fileurl + res.url)
      this.$emit('input', this.imageUrl.join(','))
      this.$forceUpdate()
    }
  }
}
</script>

<style lang="scss" scoped>
    @import "~@/styles/mixin.scss";
    .upload-container {
        width: 100%;
        position: relative;
        @include clearfix;
        .image-uploader {
            width: 400px;
        }
        .image-preview {
            position: relative;
            .image-preview-wrapper {
                position: relative;
                width: 200px;
                height: 200px;
                border:1px solid #d9d9d9;
                display: inline-block;
                img {
                    width: 100%;
                    height: 100%;
                }
                &:hover {
                    .image-preview-action {
                        opacity: 1;
                    }
                }
            }
            .image-preview-action {
                position: absolute;
                width: 100%;
                height: 100%;
                left: 0;
                top: 0;
                cursor: default;
                text-align: center;
                color: #fff;
                opacity: 0;
                font-size: 20px;
                background-color: rgba(0, 0, 0, .5);
                transition: opacity .3s;
                cursor: pointer;
                text-align: center;
                line-height: 200px;
                .el-icon-delete {
                    font-size: 36px;
                }
            }
        }
    }

</style>
